import {createRouter, createWebHistory} from 'vue-router'
import {Notify} from "vant";
import store from '../store'
import AddressEdit from "../views/profile/address/AddressEdit";
const Home = () =>
    import ('../views/home/Home')
const Category = () =>
    import ('../views/category/Category')
const Detail = () =>
    import ('../views/detail/Detail')
const Profile = () =>
    import ('../views/profile/Profile')
const Shopcart = () =>
    import ('../views/shopcart/ShopCart')
const Register = () =>
    import ('../views/profile/Register')
const Login = () =>
    import ('../views/profile/Login')
const AddressList = () =>
    import ('../views/profile/address/AddressList')
const CreateOrder = () =>
    import ('../views/profile/orders/CreateOrder')
const Order = () =>
    import ('../views/profile/orders/Order')
const OrderDetail = () =>
    import ('../views/profile/orders/OrderDetail')


const routes = [{
    path: '',
    name: 'DefaultHome',
    component: Home,
    meta: {
        title: '图书兄弟-首页'
    }
}, {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
        title: '图书兄弟-首页'
    }
},
    {
        path: '/category',
        name: 'Category',
        component: Category,
        meta: {
            title: '图书兄弟-商品分类'
        }
    },
    {
        path: '/detail',
        name: 'Detail',
        component: Detail,
        meta: {
            title: '图书兄弟-商品详情'
        }
    },
    {
        path: '/shopcart',
        name: 'ShopCart',
        component: Shopcart,
        meta: {
            title: '图书兄弟-购物车',
            isAuthRequired: true// 是否授权判断
        }
    },
    {
        path: '/profile',
        name: 'Profile',
        component: Profile,
        meta: {
            title: '图书兄弟-个人中心',
            isAuthRequired: true// 是否授权判断
        }
    },
    {
        path: '/register',
        name: 'Register',
        component: Register,
        meta: {
            title: '图书兄弟-用户注册'
        }
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: {
            title: '图书兄弟-用户登录'
        }
    },
    {
        path: '/addresslist',
        name: 'AddressList',
        component: AddressList,
        meta: {
            title: '图书兄弟-地址管理',
            isAuthRequired: true// 是否授权判断
        }
    },
    {
        path: '/addressedit',
        name: 'AddressEdit',
        component: AddressEdit,
        meta: {
            title: '图书兄弟-新增地址',
            isAuthRequired: true// 是否授权判断
        }
    },
    {
        path: '/createorder',
        name: 'CreateOrder',
        component: CreateOrder,
        meta: {
            title: '图书兄弟-创建订单',
            isAuthRequired: true// 是否授权判断
        }
    },
    {
        path: '/order',
        name: 'Order',
        component: Order,
        meta: {
            title: '图书兄弟-我的订单',
            isAuthRequired: true// 是否授权判断
        }
    },
    {
        path: '/orderdetail',
        name: 'OrderDetail',
        component: OrderDetail,
        meta: {
            title: '图书兄弟-订单详情',
            isAuthRequired: true// 是否授权判断
        }
    }

]


const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
    // 如果没有登录，在这里到Login
    if (to.meta.isAuthRequired && store.state.user.isLogin === false) {
        Notify('你还没有登录，请先登录')
        return next('/login')
    } else {
        next()
    }
    document.title = to.meta.title
})

export default router